package com.api.service.impl;

import java.time.LocalDateTime;
import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import com.api.common.Common;
import com.api.common.reqres.Request;
import com.api.common.reqres.Response;
import com.api.component.RequestComponent;
import com.api.component.ResponseComponent;
import com.api.mapper.ShippingAddressMapper;
import com.api.model.entity.ShippingAddress;
import com.api.model.request.NullRequest;
import com.api.model.request.ShippingAddressInsertRequest;
import com.api.model.request.ShippingAddressUpdateRequest;
import com.api.model.response.NullResponse;
import com.api.service.ShippingAddressService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author cfp
 * @since 2019-10-14
 */
@Service
public class ShippingAddressServiceImpl extends ServiceImpl<ShippingAddressMapper, ShippingAddress>
		implements ShippingAddressService {

	@Autowired
	private ShippingAddressMapper shippingAddressMapper;
	@Autowired
	private RequestComponent requestComponent;
	@Autowired
	private ResponseComponent responseComponent;

	@Override
	public Response<NullResponse> shippingAddressList(Request<NullRequest> request) {
		// TODO Auto-generated method stub
		String userid = request.getHead().getUserid();
		LambdaQueryWrapper<ShippingAddress> queryWrapper = new LambdaQueryWrapper<ShippingAddress>();
		queryWrapper.eq(ShippingAddress::getUserid, Long.valueOf(userid));
		queryWrapper.orderByDesc(ShippingAddress::getCreatetime);
		List<ShippingAddress> shippingAddressList = shippingAddressMapper.selectList(queryWrapper);
		if (CollectionUtils.isEmpty(shippingAddressList)) {
			return responseComponent.toError("没有绑定收货地址!");
		}
		return responseComponent.toSuccess(shippingAddressList);
	}

	@Override
	public Response<NullResponse> shippingAddressUpdate(Request<ShippingAddressUpdateRequest> request) {
		// TODO Auto-generated method stub
		requestComponent.checkBody(request.getBody(), ShippingAddressUpdateRequest.class);
		ShippingAddressUpdateRequest update = request.getBody();
		// 设置更新主键
		LambdaQueryWrapper<ShippingAddress> queryWrapper = new LambdaQueryWrapper<ShippingAddress>();
		queryWrapper.eq(ShippingAddress::getOid, update.getOid());
		// 设置更新对象
		ShippingAddress entity = new ShippingAddress();
		BeanUtils.copyProperties(update, entity);
		entity.setUpdatetime(LocalDateTime.now());
		entity.setUpdateuser(request.getHead().getUserid());

		shippingAddressMapper.update(entity, queryWrapper);
		return responseComponent.toSuccess(null);
	}

	@Override
	public Response<NullResponse> shippingAddressInsert(Request<ShippingAddressInsertRequest> request) {
		// TODO Auto-generated method stub
		requestComponent.checkBody(request.getBody(), ShippingAddressInsertRequest.class);
		ShippingAddressInsertRequest insert = request.getBody();
		ShippingAddress entity = new ShippingAddress();
		BeanUtils.copyProperties(insert, entity);
		entity.setUserid(Long.valueOf(request.getHead().getUserid()));
		entity.setCreatetime(LocalDateTime.now());
		entity.setCreateuser(Common.SYSTEM);
		shippingAddressMapper.insert(entity);
		return responseComponent.toSuccess(null);
	}

}
